package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Region;

import androidx.core.content.ContextCompat;

import tw.com.hitevision.whiteboard.android.R;
import tw.com.hitevision.whiteboard.android.state.AutoshapeProperty;

public class Eraser extends Autoshape {
    private int strokeColor;
    private int fillColor;
    private boolean visible;
    private DashPathEffect dashPathEffect;
    private long lastRenderTime;

    public Eraser(Context context, float x, float y, AutoshapeProperty.Type type, boolean fill) {
        super(context, x, y, type, 0, fill, 1);

        strokeColor = ContextCompat.getColor(context, R.color.eraserStroke);
        fillColor = ContextCompat.getColor(context, R.color.eraserFill);
        dashPathEffect = new DashPathEffect(new float[] {8.0f, 8.0f}, 0);
        visible = false;
    }

    @Override
    public void getRegion(Region out) {
        path.computeBounds(detectRectF, true);

        detectRectF.roundOut(detectRect);

        detectClipRegion.set(detectRect);
        out.setPath(path, detectClipRegion);
    }

    @Override
    public void render(Canvas canvas, Paint paint, Context context) {
        if (visible) {
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeWidth(2);
            paint.setPathEffect(dashPathEffect);
            paint.setColor(strokeColor);
            canvas.drawPath(path, paint);
            paint.setPathEffect(null);

            paint.setStyle(Paint.Style.FILL);
            paint.setColor(fillColor);
            canvas.drawCircle(getCenterX(), getCenterY(), getRadius(), paint);

//            checkRender(System.currentTimeMillis());
        }
    }

    public void setLastRenderTime(long t) {
        lastRenderTime = t;
    }

    public long getLastRenderTime() { return lastRenderTime; }

    private void checkRender(long t) {
        if (t - lastRenderTime > 500) {
            visible = false;
        }
    }

    public void setVisible(boolean t) {
        visible = t;
    }

    public boolean isVisible() {
        return visible;
    }

}
